package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"origin/model/mysql"
	"origin/originrpc/internal/svc"
	"origin/originrpc/originrpc"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 短信
func (l *RegisterLogic) Register(in *originrpc.RegisterRequest) (*originrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var phone mysql.Phone
	mysql.DB.Debug().Where("phone = ?", in.Phone).Find(&phone)
	if phone.ID != 0 {
		return nil, fmt.Errorf("手机号已注册,请重新输入")
	}
	m := mysql.Phone{
		Phone:    in.Phone,
		Password: in.Password,
	}
	err := mysql.DB.Debug().Create(&m).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败")
	}

	return &originrpc.RegisterResponse{
		Id: int64(m.ID),
	}, nil
}
